﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Diploma.Web.Areas.Account.Controllers
{
    public class FileController : Controller
    {
        public ActionResult FileUpload()
        {
            string fileName = "";
            try
            {
                HttpFileCollectionBase hfc = HttpContext.Request.Files;
                string path = "content/uploaded";

                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFileBase hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {

                        if (Request.Browser.Browser == "IE")
                        {
                            fileName = Path.GetFileName(hpf.FileName);
                        }
                        else
                        {
                            fileName = System.IO.Path.GetRandomFileName() + hpf.FileName;
                        }
                        string fullPathWithFileName = Server.MapPath("~/"+Path.Combine(path, fileName));
                        hpf.SaveAs(fullPathWithFileName);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return Json(Url.Content("~/Content/Uploaded/" + fileName));
        }

    }
}
